<template>
  <div>
    <div style="margin-bottom:10px;" />
    <el-form label-width="90px" size="small" :inline="true">
      <el-form-item label="任务组名称">
        <el-input v-model="taskName" placeholder="请填写任务组名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getGroupList">搜索</el-button>
        <el-button type="primary" @click="createTeam">新建</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="columnList" border>
      <el-table-column prop="name" label="名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
          <i v-if="scope.row.order === 0" class="el-icon-star-on" />
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间" :formatter="dateFormatter" />
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isSys !== 1" type="primary" size="mini" @click="editTeamInfo(scope)">编辑</el-button>
          <el-button v-if="scope.row.isSys !== 1" type="danger" size="mini" @click="deleteTeam(scope)">删除</el-button>
          <el-button type="primary" size="mini" :disabled="scope.row.order === 0" @click="setTheDefault(scope)">设为默认</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px;text-align:center;">
      <el-pagination
        :current-page="pageInfo.pageNo"
        :page-sizes="[ 20, 30, 40,50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 新建/编辑任务组信息 -->
    <el-dialog :visible.sync="teamFlag" :title="handleTeamTitle">
      <el-form ref="groupForm" :model="teamInfo" :rules="rules" label-width="80px" size="small">
        <el-form-item label="任务组名" prop="name">
          <el-input v-model="teamInfo.name" placeholder="请输入任务组名称" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="primary" @click="sureEdit('groupForm')">确定</el-button>
        <el-button size="small" @click="cancleHandle('groupForm')">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { GROUP_API } from '@/api/index'
import { getFormartDate } from '@/utils/tools'
export default {
  data() {
    return {
      taskName: '',
      columnList: [{
        id: 1,
        name: '系统分组',
        createDate: '2020-04-02T09:29:03.000+0800',
        modifyDate: '2020-04-02T09:29:03.000+0800',
        order: 0,
        isSys: 1,
        typeId: null
      }],
      teamFlag: false,
      HnadleTeamType: 0,
      teamInfo: {
        name: '',
        order: 1
      },
      rules: {
        name: [{ required: true, message: '请填写任务组名称', trigger: 'blur' }]
      },
      pageInfo: {
        pageNo: 1,
        pageSize: 20
      },
      total: 0
    }
  },
  computed: {
    handleTeamTitle() {
      if (this.HnadleTeamType === 0) {
        return '新建任务组'
      } else {
        return '编辑任务组'
      }
    }
  },
  mounted() {
    this.getGroupList()
  },
  methods: {
    // 时间格式化
    dateFormatter(row, col, cellValue) {
      return getFormartDate(cellValue)
    },
    getGroupList() {
      const info = _.cloneDeep(this.pageInfo)
      if (this.taskName) {
        info.name = this.taskName
      }
      GROUP_API.getGroupList(info).then(res => {
        this.total = res.data.totalRows
        this.columnList = res.data.data
      })
    },
    //   分页显示条数改变
    handleSizeChange(size) {
      this.pageInfo.pageSize = size
      this.getGroupList()
    },
    // 页码改变
    handleCurrentChange(page) {
      this.pageInfo.pageNo = page
      this.getGroupList()
    },
    //   创建任务组
    createTeam() {
      this.HnadleTeamType = 0
      this.teamFlag = true
    },
    // 确定编辑
    sureEdit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          const info = _.cloneDeep(this.teamInfo)
          if (this.HnadleTeamType === 0) {
            GROUP_API.createGroup(info).then(res => {
              this.$message({ type: 'success', message: '创建成功', center: true })
              this.getGroupList()
              this.teamFlag = false
            })
          } else {
            GROUP_API.editGroup(info).then(res => {
              this.$message({ type: 'success', message: '编辑成功', center: true })
              this.getGroupList()
              this.teamFlag = false
            })
          }
          this.$refs[name].resetFields()
        }
      })
    },
    cancleHandle(name) {
      this.teamFlag = false
      this.$refs[name].resetFields()
    },
    editTeamInfo(scope) {
      this.teamInfo = scope.row
      this.HnadleTeamType = 1
      this.teamFlag = true
    },
    // 删除任务组
    deleteTeam(scope) {
      this.$confirm('您确定要删除此任务组吗', '系统提示', { type: 'warning' }).then(() => {
        GROUP_API.deleteGroup(scope.row.id).then(res => {
          this.$message({ type: 'success', center: true, message: '删除成功' })
          this.getGroupList()
        })
      }).catch(() => {})
    },
    // 设置为默认任务组
    setTheDefault(scope) {
      this.$confirm('您确定要将此任务组设置为默认任务组吗？', '系统提示', { type: 'warning' }).then(() => {
        GROUP_API.setGroupToDefault(scope.row.id).then(res => {
          this.$message({ type: 'success', center: true, message: '设置成功' })
          this.getGroupList()
        })
      }).catch(() => {})
    }
  }
}
</script>
